//
//  EditorTableViewCell.m
//  CMVRFForIOS
//
//  Created by zhangnorman on 15/4/8.
//  Copyright (c) 2015年 hvacapp. All rights reserved.
//

#import "EditorTableViewCell.h"
#import "UITapView.h"

@interface EditorTableViewCell()
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) UILabel *infoLabel;
@property (nonatomic,strong) UILabel *selectCity;
@property (nonatomic,strong) UILabel *unitLabel;
@property (nonatomic,strong) UILabel *starLabel;

@property (nonatomic,strong) UITapView *clickView;

@end

@implementation EditorTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setupViews];
    }
    return self;
}

- (void)setupViews {
    self.contentView.width = kScreen_Width - CELL_PADDING_RIGTH_AND_IMAGE;
    //标题
    self.titleLabel = [UILabel labelSingleLineFontSize:kListTitleFontSize content:@"房间面积（平米）  "];
    self.titleLabel.x = CELL_PADDING_LEFT;
    self.titleLabel.y = CELL_PADDING_TOP;
    self.titleLabel.textColor = CM_TABLE_TITLE_COLOR;
    [self.contentView addSubview:self.titleLabel];
    
    //设置cell的高度
    self.contentView.height = CGRectGetMaxY(self.titleLabel.frame) + CELL_PADDING_BOTTOM;
    
    //选择城市提示
    self.selectCity = [UILabel labelSingleLineFontSize:10 content:@"请选择城市"];
    self.selectCity.x = self.contentView.width - self.selectCity.width;
    self.selectCity.centerY = self.contentView.height/2;
    self.selectCity.textColor = CM_NAVIGATION_RED_COLOR;
    [self.contentView addSubview:self.selectCity];
    
    //信息label
    self.infoLabel = [UILabel labelSingleLineFontSize:kListDetailFontSize content:@"信息"];
    self.infoLabel.x = CGRectGetMaxX(self.titleLabel.frame) + CELL_CONTENT_MARGIN;
    self.infoLabel.width = kScreen_Width - self.infoLabel.x - 2*CELL_CONTENT_MARGIN;
    self.infoLabel.y = CELL_PADDING_TOP;
    self.infoLabel.textColor = CM_TABLE_DETAIL_COLOR;
    [self.contentView addSubview:self.infoLabel];
    
    //输入框
    self.editorView = [[UITextField alloc]initWithFrame:CGRectMake(CGRectGetMaxX(self.titleLabel.frame) + CELL_CONTENT_MARGIN, CELL_PADDING_TOP, self.contentView.width - CGRectGetMaxX(self.titleLabel.frame) - 2*CELL_CONTENT_MARGIN, self.titleLabel.height)];
    self.editorView.textColor = CM_TABLE_DETAIL_COLOR;
    self.editorView.font = [UIFont systemFontOfSize:kListTitleFontSize];
    self.editorView.keyboardType=UIKeyboardTypeDefault;//设置键盘类型为默认的
    self.editorView.returnKeyType=UIReturnKeyDone;//返回键的类型
    self.editorView.textAlignment = NSTextAlignmentRight;
    
    NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
    style.alignment = NSTextAlignmentCenter;
    
    
    UILabel* placeholderLabel = [self.editorView valueForKey:@"_placeholderLabel"];
    placeholderLabel.textAlignment=NSTextAlignmentLeft;
    
    [self.editorView addTarget:self action:@selector(textFieldChange) forControlEvents:UIControlEventEditingChanged];
    [self.editorView addTarget:self action:@selector(textFieldEditingEnd) forControlEvents:UIControlEventEditingDidEnd];
    
    [self.contentView addSubview:self.editorView];
    
    //必填项目
    self.starLabel = [UILabel labelSingleLineFontSize:20 content:@"*"];
    self.starLabel.textColor = CM_NAVIGATION_RED_COLOR;
    self.starLabel.x = CGRectGetMaxX(self.editorView.frame) + 1;
    self.starLabel.centerY = CGRectGetMidY(self.editorView.frame);
    self.starLabel.hidden = YES;
    [self.contentView addSubview:self.starLabel];
    
    //单位
    self.unitLabel = [UILabel labelSingleLineFontSize:kListTitleFontSize content:@"danwei"];
    self.unitLabel.textColor = CM_TABLE_TITLE_COLOR;
    self.unitLabel.x = CGRectGetMaxX(self.editorView.frame) + 1;
    self.unitLabel.centerY = CGRectGetMidY(self.editorView.frame);
    self.unitLabel.hidden = YES;
    [self.contentView addSubview:self.unitLabel];
    
    self.clickView = [[UITapView alloc]initWithFrame:CGRectMake(0, 0, kScreen_Width, self.contentView.height)];
    @weakObj(self)
    [self.clickView addTargetsBlock:^(id obj) {
        [Weakself.editorView becomeFirstResponder];
    }];
    [self.contentView addSubview:self.clickView];
}

- (CGFloat)getHeight {
    return self.contentView.height;
}

- (void)setTitleStr:(NSString*)titleStr andColor:(UIColor*)titleColor andHintStr:(NSString*)hintStr andInputViewIsHidden:(BOOL)hidden andInfoStr:(NSString*)infoStr andUnit:(NSString*)unitStr{
    if (![infoStr isKindOfClass:[NSNull class]] && infoStr != nil) {
        CGSize size = [Tools sizeOfSingleLineText:titleStr withSize:kListTitleFontSize];
        self.titleLabel.size = size;
        
        size = [Tools sizeOfSingleLineText:infoStr withSize:kListDetailFontSize];
        self.infoLabel.size = size;
        self.infoLabel.x = CGRectGetMaxX(self.titleLabel.frame) + CELL_CONTENT_MARGIN;
        self.infoLabel.centerY = CGRectGetMidY(self.titleLabel.frame);
        if (CGRectGetMaxX(self.infoLabel.frame) > self.selectCity.x) {
            self.infoLabel.width = self.selectCity.x - self.infoLabel.x - CELL_CONTENT_MARGIN;
        }
    }
    
    if (hidden) {
        self.selectCity.hidden = NO;
        self.editorView.hidden = YES;
        self.clickView.hidden = YES;
        self.infoLabel.hidden = NO;
    }
    else {
        self.selectCity.hidden = YES;
        self.editorView.hidden = NO;
        self.clickView.hidden = NO;
        self.infoLabel.hidden = YES;
    }
    
    if ([Tools isEmpty:unitStr]) {
        self.unitLabel.hidden = YES;
    } else {
        self.unitLabel.hidden = NO;
        self.unitLabel.text = unitStr;
    }
    
    self.editorView.hidden = hidden;
    self.titleLabel.text = titleStr;
    if (titleColor != nil) {
        self.titleLabel.textColor = titleColor;
    } else {
        self.titleLabel.textColor = CM_TABLE_TITLE_COLOR;
    }
    
    self.infoLabel.text = infoStr;
    
    if (![Tools isEmpty:hintStr]) {
        NSMutableParagraphStyle *style = [[NSMutableParagraphStyle alloc] init];
        style.alignment = NSTextAlignmentCenter;
        NSAttributedString *attri = [[NSAttributedString alloc] initWithString:hintStr attributes:@{NSForegroundColorAttributeName:CM_PLACEHOLDER_TEXT_COLOR,NSFontAttributeName:[UIFont systemFontOfSize:kPlaceHolderFontSize], NSParagraphStyleAttributeName:style}];
        self.editorView.attributedPlaceholder = attri;
    }
    
    //调整宽度和位置
    self.titleLabel.width = [Tools sizeOfSingleLineText:titleStr withSize:kListTitleFontSize].width + 2;
    self.starLabel.x = CGRectGetMaxX(self.titleLabel.frame);
    self.editorView.x = CGRectGetMaxX(self.starLabel.frame) + 5;
    if ([Tools isEmpty:unitStr]) {
        self.editorView.width = kScreen_Width - 15 - self.editorView.x - 5;
    } else {
        self.unitLabel.width = [Tools sizeOfSingleLineText:unitStr withSize:kListTitleFontSize].width + 5;
        self.unitLabel.x = kScreen_Width - 15 - self.unitLabel.width;
        self.editorView.width = self.unitLabel.x - self.editorView.x - 5;
    }
    self.editorView.inputAccessoryView = [self addToolbar];
    //    self.editorView.placeholder = hintStr;
}

- (UIToolbar *)addToolbar
{
    UIToolbar *toolbar = [[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, kScreen_Width, 35)];
    toolbar.tintColor = [UIColor blueColor];
    toolbar.backgroundColor = [UIColor colorWithRed:160/255.0 green:160/255.0 blue:160/255.0 alpha:1.0];
//    UIBarButtonItem *nextButton = [[UIBarButtonItem alloc] initWithTitle:@"下一步" style:UIBarButtonItemStylePlain target:self action:@selector(nextTextField)];
//    UIBarButtonItem *prevButton = [[UIBarButtonItem alloc] initWithTitle:@"上一步" style:UIBarButtonItemStylePlain target:self action:@selector(prevTextField)];
    UIBarButtonItem *space = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
    UIBarButtonItem *bar = [[UIBarButtonItem alloc] initWithTitle:@"完成" style:UIBarButtonItemStylePlain target:self action:@selector(textFieldDone)];
    toolbar.items = @[space, bar];
    return toolbar;
}

- (void)nextTextField {
    [Tools showMessageCenter:@"nextTextField"];
}

- (void)prevTextField {
    [Tools showMessageCenter:@"nextTextField"];
}

- (void)textFieldDone {
    [self.editorView resignFirstResponder];
}

- (void)setItemIsNesessory {
    self.starLabel.hidden = NO;
}

- (void)setEditorText:(NSString *)editText {
    if (![editText isKindOfClass:[NSNull class]] && editText != nil) {
        self.editorView.text = editText;
    }
}

- (void)setNavigationHint:(NSString*) navigationHintStr {
    CGSize size = [Tools sizeOfSingleLineText:navigationHintStr withSize:12];
    self.selectCity.size = size;
    self.selectCity.text = navigationHintStr;
    self.selectCity.x = kScreen_Width - CELL_PADDING_RIGTH_AND_IMAGE - self.selectCity.width;
}

- (void)setKeyboardType:(UIKeyboardType)type {
    self.editorView.keyboardType = type;
}

- (void)textFieldChange{
    if (self.TextFiledValueBlock) {
        self.TextFiledValueBlock(self.editorView.text);
    }
}

- (void)textFieldEditingEnd{
    if (self.TextFiledEditingEndBlock) {
        self.TextFiledEditingEndBlock(self.editorView.text);
    }
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
